<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;

class WaRoles
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_roles';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 角色列表 - 分页
     * @return false|string
     */
    public static function list($page,$pageNum,$fields,$condition)
    {

        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wa_roles as roles')
            ->leftJoin('wa_admins as admin', 'admin.id', '=', 'roles.admin_id')
            ->select($fields)
            ->where($condition);
        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('roles.id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);

        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     * 角色列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields,$condition)
    {

        $list = Db::table('wa_roles as roles')
            ->leftJoin('wa_admins as admin', 'admin.id', '=', 'roles.admin_id')
            ->select($fields)
            ->where($condition)
            ->orderBy('roles.id', 'asc')
            ->get();

        return json_encode($list);
    }




    public static function find($condition)
    {
        $res = Db::table('wa_roles')
            ->where($condition)
            ->first();
        return json_encode($res);
    }

    public static function created($data){

        return Db::table('wa_roles')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wa_roles')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition){

        $res = Db::table('wa_roles')->where($condition)->delete();
        return json_encode($res);

    }



}